package com.zenithsun.pic.picreview.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.syzg.common.controller.BaseController;
import com.syzg.common.util.SearchMap;
import com.syzg.common.model.WebObj;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zenithsun.common.bean.ModelType;
import com.zenithsun.common.bean.OutMessage;
import com.zenithsun.common.bean.OutMessageState;
import com.zenithsun.common.review.model.ReviewState;
import com.zenithsun.common.review.model.Reviewdetail;
import com.zenithsun.common.review.service.ReviewService;
import com.zenithsun.common.review.service.ReviewdetailService;
import com.zenithsun.common.security.csrf.CSRFTokenManager;
import com.zenithsun.common.security.sql.SqlInjectionManager;
import com.zenithsun.news.newsinfo.model.Newsdetailinfo;
import com.zenithsun.pic.picdetail.model.Picdetail;
import com.zenithsun.pic.picdetail.service.PicdetailService;
import com.zenithsun.pic.picreview.model.PicReview;
import com.zenithsun.pic.picreview.service.PicReviewService;
import com.zenithsun.pic.picsubject.model.Picsubject;
/**
 * 新闻审核
 * @author wanglei@enersun.com.cn
 * @createTime 2016-6-12 09:56:53
 */
@Controller
@RequestMapping("/path/picReview")
public class PicReviewController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(PicReviewController.class);
	
	@Resource
	private PicReviewService picReviewService;
	
	@Resource
	private PicdetailService picdetailService;
	
	@Resource
	private ReviewdetailService reviewdetailService;
	@Resource
	private ReviewService reviewService;
	/** 
	 * 设置共用查询条件
	 * @return SearchMap
	 */
	
	@SuppressWarnings("unused")
	private void setShareSearchMap(SearchMap searchMap){
		searchMap.eq("a.busMark", ModelType.Pic.toString());
		searchMap.createAlias("a.picSubject", "picSubject");
	}
	/**
	 * 未审核新闻列表
	 * @param webObj
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/toListReview")
	public ModelAndView listreview(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/pic/review/listreview.jsp");
        return mv;
	}
	
	@RequestMapping("/viewreview")
	public ModelAndView viewreview(String id) throws Exception {
		SqlInjectionManager.validateSqlParam(id);
		ModelAndView mv = new ModelAndView("/zenithsun/pic/review/viewreview.jsp");
		try {
			HashMap<String, String> reviewVO = picReviewService.findPicReview(id);
			mv.addObject("review",reviewVO);
			List<Reviewdetail> reviewdetailList=reviewdetailService.listCurrReview(id);
			mv.addObject("reviewdetail", reviewdetailList);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	/**
	 * 根据状态确定进入哪一个页面
	 * @param state 不允许为空
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/initlistreview")
	public ModelAndView initlistreview(String state,HttpServletRequest request)throws Exception{
		//CSRFTokenManager.validateForm(request);
		ModelAndView mv = new ModelAndView("/zenithsun/pic/review/listreviewpass.jsp");
		if(StringUtils.isNotBlank(state)){
			if(ReviewState.WaitReview.toString().equals(state)){
				mv.setViewName("/zenithsun/pic/review/listnotreview.jsp");
			}else if(ReviewState.ReviewFail.toString().equals(state)){
				mv.setViewName("/zenithsun/pic/review/listreviewnopass.jsp");
			}else if(ReviewState.ReviewSuccess.toString().equals(state)){
				mv.setViewName("/zenithsun/pic/review/listreviewpass.jsp");
			}
		}
        mv.addObject("state",state);
        return mv;
	}
	
	/**
	 * 查询图片库，返回json
	 * @param pr
	 * @param limit 页行数
	 * @param pageNo 页码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listreviewjson", produces = "application/json;charset=UTF-8")
	@ResponseBody 
	public String listreviewjson(PicReview pr,int limit,int pageNo)throws Exception{
		OutMessage outMessage = new OutMessage();
		
		String content = "";
		try {
			PageResult pageResult = picReviewService.queryPicSubjectByState(pageNo, limit, pr);
			outMessage.setContent(pageResult);
			outMessage.setState(OutMessageState.Success);
			content = JSON.toJSONString(outMessage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}
	
	@RequestMapping("/showPictures")
	public ModelAndView listPicdetail(String picsubjectId)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/pic/review/listPictures.jsp");
		SearchMap searchMap = SearchMap.forClass(Picdetail.class);
		searchMap.eq("a.picsubject.id", picsubjectId);
		searchMap.addOrder("orderNo",searchMap.ORDER_ASC);
		@SuppressWarnings("unchecked")
		List<Picdetail> lstPicdetail = picdetailService.findObjects(searchMap);
        mv.addObject("lstPicdetail",lstPicdetail);
        return mv;
	}
	@RequestMapping("/viewPicdetail")
	public ModelAndView viewPicdetail(Picdetail picdetail,ModelAndView mv) throws Exception {
		mv.setViewName("/zenithsun/pic/review/viewPicdetail.jsp");
		try {
			String id = picdetail.getId();
			Picdetail picdetailVO = picdetailService.findObjectById(id);
			mv.addObject("picdetail",picdetailVO);
			SearchMap searchMap = SearchMap.forClass(Picsubject.class);
			mv.addObject("lstPicsubject", picdetailService.findObjects(searchMap));
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
}